package com.suntop.carousel.qlmediaplay.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;


import com.suntop.carousel.qlmediaplay.cache.CacheListener;
import com.suntop.carousel.qlmediaplay.cache.HttpProxyCacheServer;
import com.suntop.carousel.qlmediaplay.cache.StorageUtils;
import com.suntop.carousel.qlmediaplay.cache.file.FileNameGenerator;

import java.io.File;
@SuppressLint("NewApi")
public class CacheTools {
    private CacheTools() {
    }

    private static CacheTools inStance;
    private File externalFilesDir = null;

    public static CacheTools getInStance() {
        if (inStance == null) {
            inStance = new CacheTools();
        }
        return inStance;
    }

    private static HttpProxyCacheServer sharedProxy;


    public void init(File fileRoot) {
        externalFilesDir = fileRoot;
    }

    public HttpProxyCacheServer getProxy(Context context) {
        return sharedProxy == null ? (sharedProxy = newProxy(context)) : sharedProxy;
    }

    private HttpProxyCacheServer newProxy(Context context) {
        if (externalFilesDir == null) {
            externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
            if (!externalFilesDir.exists()){
                boolean mkdirs = externalFilesDir.mkdirs();
            }
        }
        HttpProxyCacheServer httpProxyCacheServer = new HttpProxyCacheServer.Builder(context.getApplicationContext())
                .maxCacheFilesCount(100)
                .cacheDirectory(externalFilesDir)
                .fileNameGenerator(new FileNameGenerator() {
                    @Override
                    public String generate(String url) {
                        return "Cache_" + FileUnit.takeFileName(url);
                    }
                })
                .build();
        return httpProxyCacheServer;
    }

    public void stopCacheService(Context context){
        getProxy(context).shutdown();
    }

    public boolean isCacheForUrl(Context context,String url){
      return  getProxy(context).isCached(url);
    }

    public String getCacheForUrl(Context context,String url){
      return getProxy(context).getCacheFile(url).getAbsolutePath();
    }



    public String registerListener(Context context, String url, CacheListener listener) {
        getProxy(context).registerCacheListener(listener, url);
        return getProxy(context).getProxyUrl(url);
    }

    public void unRegisterListener(Context context, String url, CacheListener listener) {
        getProxy(context).unregisterCacheListener(listener, url);
    }

    public void clearCache(Context context) {
        File cacheRoot = getProxy(context).getCacheRoot();
        StorageUtils.deleteFiles(cacheRoot);
    }

    /**
     * 删除url对应默认缓存文件
     *
     * @return 返回缓存是否删除成功
     */
    public boolean clearDefaultCache(Context context, String url) {
        getProxy(context);
        File pathTmp = sharedProxy.getTempCacheFile(url);
        File path = sharedProxy.getCacheFile(url);
        return StorageUtils.deleteFile(pathTmp.getAbsolutePath()) &&
                StorageUtils.deleteFile(path.getAbsolutePath());

    }

}
